package io.xxx.sync.core

import java.time.LocalDateTime
import java.time.OffsetTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.*

val zoneId: ZoneId = ZoneId.systemDefault()
val zoneOffset: ZoneOffset = OffsetTime.now().offset

val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

fun Date?.toLocalDateTime(): LocalDateTime? {
    return if (this == null) null else LocalDateTime.ofInstant(this.toInstant(), zoneId)
}

fun Long?.toLocalDateTime(nanoOfSecond: Int = 0): LocalDateTime? {
    return if (this == null) null else LocalDateTime.ofEpochSecond(this, nanoOfSecond, zoneOffset)
}

fun LocalDateTime.toDate(): Date {
    return Date.from(this.toInstant(zoneOffset))
}